<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : ucenter.class.php   FILE_PATH : E:\www\_v25\classes\application\ucenter.class.php
 * ....ucenter会员整合操作类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Sat Sep 26 14:16:43 CST 2009
 */
class ucenterClass extends FrameWork {
	/**
	 * 抛出错误信息
	 *
	 * @var obj
	 */
	var $uc_error = '';
	
	/**
	 * 构造函数
	 * @author workflow Develop Team     
	 * @param  
	 * @return bool
	 */
	function ucenterClass(){
		$this->__construct();
	}
	function __construct(){
		define('API_SYNLOGIN', 1);        		//同步登录 API 接口开关
		define('API_SYNLOGOUT', 1);        		//同步登出 API 接口开关
		//define('UC_API', TRUE);
		define('IN_UC', TRUE);
		define('UC_CONNECT', 'mysql');
		//初始化变量
		$this->_getConfiginfo();
		define('UC_API', $this->_configinfo['ucenter']['uc_api']);
		define('UC_CONNECT', $this->_configinfo['ucenter']['uc_connect']);
		define('UC_DBHOST', $this->_configinfo['ucenter']['uc_dbhost']);
		define('UC_DBUSER', $this->_configinfo['ucenter']['uc_dbuser']);
		define('UC_DBPW', $this->_configinfo['ucenter']['uc_dbpw']);
		define('UC_DBNAME', $this->_configinfo['ucenter']['uc_dbname']);
		define('UC_DBCHARSET', $this->_configinfo['ucenter']['uc_dbcharset']);
		define('UC_DBTABLEPRE', '`'.$this->_configinfo['ucenter']['uc_dbname'].'`'.'.'.$this->_configinfo['ucenter']['uc_dbtablepre']);
		define('UC_DBCONNECT', $this->_configinfo['ucenter']['uc_dbconnect']);
		define('UC_KEY', $this->_configinfo['api']['passport_key']);
		define('UC_CHARSET', strtoupper($this->_configinfo['ucenter']['uc_charset']));
		define('UC_IP', $this->_configinfo['ucenter']['uc_ip']);
		define('UC_APPID', $this->_configinfo['ucenter']['uc_appid']);
		define('UC_PPP', $this->_configinfo['ucenter']['uc_ppp']);
		define('UC_LINK', $this->_configinfo['ucenter']['uc_link']);
		//NC数据库内容
		define('NC_CONNECT', $this->_configinfo['database']['engine_type']);
		define('NC_DBHOST', $this->_configinfo['database']['servername_write']);
		define('NC_DBUSER', $this->_configinfo['database']['username_write']);
		define('NC_DBPW', $this->_configinfo['database']['password_write']);
		define('NC_DBNAME', $this->_configinfo['database']['databasename_write']);
		define('NC_DBCHARSET', $this->_configinfo['database']['database_charset']);
		define('NC_DBTABLEPRE', $this->_configinfo['database']['dbprefix']);
		
		require('../uc_client/client.php');
	}
	/**
	 * 检测会员是否存在
	 * @author workflow Develop Team     
	 * @param  $username 会员名
	 * @return bool
	 */
	function check_user_exist($username,$password){
		$ucresult = uc_user_checkname($username);
		if ($ucresult == 1){
			return true;
		}
		if($ucresult == -1) {
			$this->uc_error = 'profile_username_illegal';
			return false;
		} elseif($ucresult == -2) {
			$this->uc_error = 'profile_username_protect';
			return false;
		} elseif($ucresult == -3) {
			$this->uc_error = 'register_check_found';
			return false;
		}
	}
	
	/**
	 * 会员注册
	 * @author workflow Develop Team     
	 * @param  $username 会员名, $password 密码, $email 邮件
	 * @return bool
	 */
	function addUser($username, $password, $email){
		$uid = uc_user_register($username, $password, $email);
		if($uid <= 0) {
			if($uid == -1) {
				$this->uc_error = 'profile_username_illegal';
				return false;
			} elseif($uid == -2) {
				$this->uc_error = 'profile_username_protect';
				return false;
			} elseif($uid == -3) {
				$this->uc_error = 'profile_username_duplicate';
				return false;
			} elseif($uid == -4) {
				$this->uc_error = 'profile_email_illegal';
				return false;
			} elseif($uid == -5) {
				$this->uc_error = 'profile_email_domain_illegal';
				return false;
			} elseif($uid == -6) {
				$this->uc_error = 'profile_email_duplicate';
				return false;
			} else {
				$this->uc_error = 'undefined_action';
				return false;
			}
		}else {
			$this->adduid = $uid;
			return $uid;
		}
	}
	
	/**
	 * 会员登录
	 * @author workflow Develop Team     
	 * @param  $uid 会员ID
	 * @return int/bool/object/array/string
	 */
	function login($username, $password){
		list ( $uid, $uname, $pwd, $email, $repeat ) = uc_user_login($username, $password);
		//检查用户是否存在,不存在直接放入用户表
		$user_exist = $GLOBALS['db']->GetOneRow ( $username, 'member', 'login_name', '*' );
		if (empty ( $user_exist )) {
			//取用户组
			$group_array = $GLOBALS['db']->GetList('order by mg_score_lower', $obj_page, "member_group");
			//注册时间
			$reg_date = time ();
			//新增会员信息
			$member_array = array ('member_id' => $uid, 'email' => $email, 'password' => md5 ( $password ), 'login_name' => $uname, 'regist_time' => $reg_date, 'last_login_time' => $reg_date );
			$sql = "REPLACE INTO `".NC_DBTABLEPRE."member` (member_id,email,password,login_name,regist_time,last_login_time) VALUES ('" . $member_array ['member_id'] . "', '" . $member_array ['email'] . "', '" . $member_array ['password'] . "', '" . $member_array ['login_name'] . "', '" . $member_array ['regist_time'] . "', '" . $member_array ['last_login_time'] . "')";
			//取会员组信息
			$mg_id = $group_array[0]['mg_id']?$group_array[0]['mg_id']:'0';
			$sql2 = "REPLACE INTO `".NC_DBTABLEPRE."member_extend` (member_id,mg_id) VALUES ('" . $member_array ['member_id'] . "','". $mg_id ."')";
			$GLOBALS['db']->Execute ( $sql );
			$GLOBALS['db']->Execute ( $sql2 );
			return $uid;
		} else {
			if (md5 ( $password ) != $user_exist ['password']) {
				$GLOBALS['db']->Execute ( "UPDATE `".NC_DBTABLEPRE."member` SET password='" . md5 ( $password ) . "' WHERE member_id=" . $uid );
			}
			if ($email != $user_exist ['email']) {
				$GLOBALS['db']->Execute ( "UPDATE `".NC_DBTABLEPRE."member` SET email='" . $email . "' WHERE member_id=" . $uid );
			}
		}
		
		return $uid;
	}

	/**
	 * 输出通知部分
	 * @author workflow Develop Team     
	 * @param  
	 * @return bool
	 */
	function output_login($uid){
		echo '<div style="color:#FFF">';
		echo '<meta http-equiv="refresh" content="0.1;url='.$this->_configinfo['websit']['site_url'].'">';
		var_dump(uc_user_synlogin($uid));
		'</div>';
		exit;
	}

	/**
	 * 会员退出
	 * @author workflow Develop Team     
	 * @param  
	 * @return bool
	 */
	function logout(){
		echo '<div style="color:#FFF">';
		echo '<meta http-equiv="refresh" content="0.1;url='.$this->_configinfo['websit']['site_url'].'">';
		var_dump(uc_user_synlogout());
		'</div>';
		exit;
	}
	
	/**
	 * 会员修改
	 * @author workflow Develop Team     
	 * @param  
	 * @return bool
	 */
	function edit_user($cfg, $forget_pwd = '0'){
		uc_user_edit($cfg ['login_name'], $cfg ['old_password'], $cfg ['password'], $cfg['email'], 1);
		return true;
	}

	/**
	 * 信息推送
	 * @author workflow Develop Team     
	 * @param  $param 信息参数
	 * @return bool
	 */
	function uc_feed($param){
		$icon = $param['icon'];
		$uid = $param['uid'];
		$username = $param['username'];
		$title_template = $param['title_template'];
		$title_data = $param['title_data'];
		uc_feed_add($icon,$uid,$username,$title_template,$title_data);
		return true;
	}
}
?>